package com.cskaoyan.controller.kylan;

import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.bean.wx.CouponListVO;
import com.cskaoyan.bean.wx.CouponMylistVO;
import com.cskaoyan.service.kylan.WxCouponService;
import com.cskaoyan.until.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService couponService;

//    Integer userId = 1;


    /**
     * 获得优惠券信息
     *
     * @param baseParam
     * @return
     */
    @RequestMapping("list")
    public Result getCouponList(BaseParam baseParam) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();
        CouponListVO couponListVO = couponService.getCouponList(baseParam, userId);

        return Result.ok(couponListVO);
    }

    /**
     * 显示我的优惠券信息
     *
     * @param baseParam
     * @param status
     * @return
     */
    @RequestMapping("mylist")
    public Result getMyCouponList(BaseParam baseParam, Integer status) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();

        CouponMylistVO couponMylistVO = couponService.getMyCouponList(baseParam, userId, status);

        return Result.ok(couponMylistVO);
    }

    /**
     * 购物车下单时查找可用优惠券list
     *
     * @param cartId
     * @param grouponRulesId
     * @return
     */
    @RequestMapping("selectlist")
    public Result selectList(Integer cartId, Integer grouponRulesId) {

        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();
        CouponMylistVO couponMylistVO = couponService.getSelectCouponList(cartId, grouponRulesId, userId);

        return Result.ok(couponMylistVO);
    }

    /**
     * 首页优惠券领取
     */
    @PostMapping("receive")
    public Result receiveCoupon(@RequestBody Map<String, Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();

        Integer couponId = map.get("couponId");
        Integer code = couponService.receiveCoupon(userId, couponId);
        if (code != 0) {
            return Result.error(740, "优惠券已经领取过");

        }
        return Result.ok();
    }

    @PostMapping("exchange")
    public Result exchange(@RequestBody Map<String, String> map) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();
        String code = map.get("code");
        Integer num = couponService.receiveByExchangeCode(userId, code);
        if (num == 0) {
            return Result.ok();
        } else if (num == 1) {
            return Result.error(740, "优惠券已兑换");
        }
        return Result.error(742, "优惠券不正确");

    }


}
